<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.org/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximumscale=1">
    <title>layui布局</title>
    <!--第一步，引入layui的资源-->
    <link rel="stylesheet" th:href="@{/resources/layui/css/layui.css}">
</head>
<body>

<!--在body下引入js资源-->
<script th:src="@{/resources/layui/layui.js}"></script>

<!--开始写，layui的资源-->
<script th:inline="none">//此处有坑，需要设置“th:inline="none"”
//一般直接写在一个js文件中
//同时，在layui中引入table表格
layui.use(['layer', 'form', 'table'], function () {
    var layer = layui.layer
        , form = layui.form
        , table = layui.table
        , $ = layui.jquery;
    var returntime1 = null;
    var tableIns = table.render({
        elem: '#outrecordTable',//指定表格的id选择器
        height: 312,
        url: '/outrecord/outrecordList',
        id: 'outrecordListTable',//表格的索引
        //表格的分页
        page: true,
        limits: [5, 10, 15, 20],
        limit: 5,
        where: {
            //传值 startDate : startDate,
            returntime1: returntime1,
        },
        //表格数据的回填方法
        done: function (res) {
            if(res.code==302){
             layer.msg(res.message)
        }
        },
        cols: [[
            {field: 'id', title: '编号', width: 80, sort: true, type: "checkbox"},
            {
                field: 'vehiclefiles', title: '车牌号码', templet: function (obj) {
                return obj.vehiclefiles.vehiclenum;
            }
            },
            {
                field: 'dept', title: '部门', templet: function (obj) {
                return obj.dept.name;
            }
            },
            {field: 'outtime', title: '出车时间'},
            {field: 'predicttime', title: '预计回车时间'},
            {
                field: 'returntime', title: '回车时间', templet: function (obj) {
                return obj.returntime == null ? '未回车' : obj.returntime;
            }
            },
            {field: 'otherpeople', title: '随行人员'},
            {
                field: 'driver', title: '驾驶员', templet: function (obj) {
                return obj.driver.name;
            }
            },
            {field: 'destination', title: '目的地'},
            {field: 'createpeople', title: '创建人员'},
            //templet 状态的判断
            {
                field: 'position', title: '车辆停放位置', templet: function (obj) {
                return obj.position == null ? '未回车' : obj.position;
            }
            },
            {title: '操作', width: 250, templet: '#outrecordBtn', fixed: 'right', align: 'center'}
        ]]
    })
    $("#removerPatch").click(function () {
        console.log(123)
        //获取所选中的复选框集合
        var checkStatus = table.checkStatus('outrecordListTable');
        var data = checkStatus.data;
        console.log(data.length)
        var uIds = [];
        if (data.length > 0) {
            for (var i in data) {
                uIds += data[i].id + ",";
            }
            layer.confirm('确定删除选中的用户？', {icon: 3, title: '提示信息'}, function
                (index) {
                $.post("/outrecord/deletePatch", {ids: uIds}, function (res) {
                    if (res.code == 3001) {
                        layer.msg(res.message);
                        tableIns.reload();
                    } else if (res.code == 309) {
                        layer.msg(res.message);
                    }
                })
            })
        }
    })
    $("#searchBtn").click(function () {
        var vehiclenum1 = $("#vehiclenum").val();
        table.reload('outrecordListTable', {url: '/outrecord/outrecordList1?vhicelenum=' + vehiclenum1})
    })

    $("#out").click(function () {
        //代开子页面 出车登记
        var index = layui.layer.open({
            //type 对应好1到五的值 不同的值可以传递不同的属性
            type: 2,
            title: '出车登记',
            content: '/outrecord/goOutrecord',
            area: ['600px', '500px'],
            success: function (layero, index) {
                var body = layer.getChildFrame('body', index);
                //少了这个是 不能从父页面向子页面传值的
                //向content的值隐藏传递值
                //这样就将值传递给子页面中id为userIdHiden的隐藏域
            },
            btn: ['确定', '取消'],
            yes: function (index, layero) {
                //子页面向符页面传值 窗口对象window["layui-layer-iframe"+index]
                var res = window["layui-layer-iframe" + index].callbackdata();
                if (res.predicttime > res.outtime) {
                    $.ajax({
                        url: '/outrecord/insertOut',
                        data: res,
                        traditional: true,
                        success: function (res) {
                            if (res.code == 303) {
                                layer.msg(res.message);
                                tableIns.reload();
                                layui.layer.close(index);
                            } else if (res.code == 304) {
                                layer.msg(res.message);
                            }
                        }
                    })
                } else {
                    layer.msg("预约返回时间必须大于出车时间")
                }
            },
            btn2: function (index, layero) {
                layui.layer.close(index);
            }
        })
    })
    table.on("tool(outrecordList)", function (obj) {
        //获取当前行的数据
        var data = obj.data;
        switch (obj.event) {
            case "updaterecord":
                if (data.returntime != null) {
                    layer.msg("已回车不能进行出车记录修改")
                } else {
                    updaterecord(data);
                }
                break;
            case "deleterecord":
                deleterecord(data);
                break;
            case "inrecord":
                if (data.returntime != null) {
                    layer.msg("已回车不能再次进行回车信息登记")
                } else {
                    inrecord(data);
                }
                break;
            case "checkdetail":
                checkdetail(data);
                break;
        }
    })

    function inrecord(data) {
        //代开子页面 回车登记
        var index = layui.layer.open({
            //type 对应好1到五的值 不同的值可以传递不同的属性
            type: 2,
            title: '回车登记',
            content: '/outrecord/goInrecord',
            area: ['600px', '500px'],
            success: function (layero, index) {
                var body = layer.getChildFrame('body', index);
                //少了这个是 不能从父页面向子页面传值的
                //向content的值隐藏传递值
                body.find("#outrecordid").val(data.id);
                //这样就将值传递给子页面中id为userIdHiden的隐藏域
            },
            btn: ['确定', '取消'],
            yes: function (index, layero) {
                //子页面向符页面传值 窗口对象window["layui-layer-iframe"+index]
                var res = window["layui-layer-iframe" + index].callbackdata();
                if (res.returntime > res.outtime) {
                    $.ajax({
                            url: '/outrecord/updateByInRecord',
                            data: res,
                            traditional: true,
                            success: function (res) {

                                if (res.code == 307) {
                                    layer.msg(res.message);
                                    tableIns.reload();
                                    layui.layer.close(index);
                                } else if (res.code == 308) {
                                    layer.msg(res.message);
                                }
                            }
                        }
                    )}
                else
                    {
                        layer.msg("回车时间必须大于出车时间");
                    }
                }
            ,
                btn2: function (index, layero) {
                    layui.layer.close(index);
                }
            })
    }

    function deleterecord(data) {
        layer.confirm('确定删除选中的车辆？', {icon: 3, title: '提示信息'}, function (index) {
            $.ajax({
                type: 'get',
                url: '/outrecord/deletByRecord',
                data: {id: data.id},
                success: function (res) {
                    if (res.code == 309) {
                        layer.msg(res.message);

                    } else if (res.code == 3001) {
                        layer.msg(res.message);
                    }
                    tableIns.reload();
                }
            })
        })
    }

    function updaterecord(data) {
        //代开子页面
        var index = layui.layer.open({
            //type 对应好1到五的值 不同的值可以传递不同的属性
            type: 2,
            title: '修改出车记录',
            content: '/outrecord/updateoutrecord',
            area: ['600px', '500px'],
            success: function (layero, index) {

                var body = layer.getChildFrame('body', index);
                //少了这个是 不能从父页面向子页面传值的
                //向content的值隐藏传递值
                body.find("#outrecordid").val(data.id);
                //这样就将值传递给子页面中id为userIdHiden的隐藏域
            },
            btn: ['确定', '取消'],
            yes: function (index, layero) {
                //子页面向符页面传值 窗口对象window["layui-layer-iframe"+index]
                var res = window["layui-layer-iframe" + index].callbackdata();
                $.ajax({
                        url: '/outrecord/updateByRecord',
                        data: res,
                        traditional: true,
                        success: function (res) {
                            if (res.code == 307) {
                                layer.msg(res.message);
                                tableIns.reload();
                                layui.layer.close(index);
                            } else if (res.code == 308) {
                                layer.msg(res.message);
                            }
                        }
                    }
                )
            },
            btn2: function (index, layero) {
                layui.layer.close(index);
            }
        })
    }
});

</script>
<blockquote class="layui-elem-quote quoteBox">
    <form class="layui-form">
        <div class="layui-inline">
            <input type="text" class="layui-input" placeholder="请输入车牌号码" id="vehiclenum">
        </div>
        <div class="layui-inline">
            <button type="button" class="layui-btn " id="searchBtn">搜索</button>
        </div>
        <div class="layui-inline">
            <button type="button" class="layui-btn " id="out">出车登记</button>
        </div>
        <div class="layui-inline">
            <button type="button" class="layui-btn " id="removerPatch">批量删除</button>
        </div>
    </form>
</blockquote>

<table id="outrecordTable" lay-filter="outrecordList">

</table>
<script type="text/html" id="outrecordBtn">
    <a class="layui-btn layui-btn-xs  layui-btn-primary" lay-event="updaterecord">修改</a>
    <a class="layui-btn layui-btn-xs  layui-btn-primary" lay-event="inrecord">回车登记</a>
    <a class="layui-btn layui-btn-xs  layui-btn-primary" lay-event="deleterecord">删除</a>
</script>

</body>
</html>